import { defineStore } from 'pinia';
import dayjs from "@/plugins/dayjs.js"
	
export const useVipStore = defineStore('vip', {
	state: ()=>{
		return {
			testData: [],
			myVipReward: {}, // 我的vip 奖池
			lotteryDraw: {}, // 用户抽奖的结果
			wsXiaomiDownBind: [], // 小米下载和绑定时候的弹窗
			wsData: {// 这是websocket 推送的数据
				reward: [], // 奖励的数据
				brand: [], // 手机品牌
			}, 
			clickDeviceTime: null, //  记录刷新设备当时的时间
			clickChildrenLocalTimeLimt: null // 如果是null 就是第一次点击，如果有时间，那么再次获取当前时间和这个时间对比，如果超过19分钟，才可点击，否则给出提示不能点击
		}
	},
	getters: {
		
	},
	//  动作支持异步
	actions: {
		setTestData(item){
			this.testData.push(item)
		},
		getTestData(){
			return this.testData
		},
		getLocalTimeLimt(){
			return this.clickChildrenLocalTimeLimt
		},
		setClickChildrenLocalTimeLimt(){
			let currentTime = dayjs();
			this.clickChildrenLocalTimeLimt = currentTime
		},
		childrenLocalIsOutTime(s = 19 * 60){ // 19分钟 // true： 要中断用户操作，因为太频繁， false： 用户可以操作
			let currentTime = dayjs();
			 let isOutTime = false; // true： 不能点击因为要过s秒才能点击  false： 已经超过s秒了，可以点击
            if (this.clickChildrenLocalTimeLimt) {
                // 计算时间差，单位为秒
                let duration = currentTime.diff(this.clickChildrenLocalTimeLimt, 'second');
                // 判断时间差是否大于 19 分钟（19 * 60 秒）
                isOutTime = duration < s
            }
			return isOutTime
		},
		setClickDeviceTime(t){
			this.clickDeviceTime = t
		},
		isOutTime(s = 5){ // s 设置间隔时间，超过这个时间就返回true
			 let currentTime = dayjs();
			 let isOutTime = false; // true： 不能点击因为要过s秒才能点击  false： 已经超过s秒了，可以点击
			 if(this.clickDeviceTime){
				 let duration = currentTime.diff(this.clickDeviceTime, 'second');
				 isOutTime = duration < s;
			 }
			 this.setClickDeviceTime(currentTime)
			 return isOutTime;
		},
		
		setWSReward(data){
			this.wsData.reward = data
		},
		haveData(){
			return this.wsData.reward.length > 0
		},
		isExistReward(id){
			return this.wsData.reward.some(item => item.data.id === id)
		},
		addRewardItem(item){
			this.wsData.reward.push(item)
		},
		removeRewardItem(id){
			const newReward = [];
			this.wsData.reward.forEach(item => {
				if(item?.data?.id !== id){
					newReward.push(item)
				}
			})
			this.wsData.reward = newReward;
		},
		findRewardItem(id){
			return this.wsData.reward.find(r => r.data.id === id)
		},
		setWSMi(data){
			this.wsXiaomiDownBind = data
		},
		haveMi(){
			return this.wsXiaomiDownBind.length > 0
		},
		isExistMi(id){
			return this.wsXiaomiDownBind.some(item => item.data.id === id)
		},
		addMiItem(item){
			this.wsXiaomiDownBind.push(item)
		},
		removeMiItem(id){
			const newMi = [];
			this.wsXiaomiDownBind.forEach(item => {
				if(item?.data?.id !== id){
					newMi.push(item)
				}
			})
			this.wsXiaomiDownBind = newMi;
		},
		findMiItem(id){
			return this.wsXiaomiDownBind.find(r => r.data.id === id)
		},
		
		setWSBrand(data){
			this.wsData.brand = data
		},
		setMyVipReward(res){
			this.myVipReward = res;
		},
		setLotteryDraw(res){
			this.lotteryDraw = res;
		},
		clearAll(){
			this.myVipReward = {}
			this.lotteryDraw = {}
		}
	}
});